{ Summer Informatics School.
  Problemset: http://agul-problems1.googlecode.com/svn/trunk/2010-2011/sis/problemset11.pdf
  Problem I
  Points: 100
}
uses SysUtils,math;
var i,n,j,ls,l,r:longint; s:string; f:array['0'..'9'] of boolean; p,a:array[0..510] of longint;
procedure swap(var a,b:longint);
var t:longint;
begin
t:=a;
a:=b;
b:=t;
end;
procedure sort(l,r:longint);
var i,j,x:longint;
begin
i:=l;
j:=r;
x:=p[(l+r) div 2];
repeat
	while p[i]<x do inc(i);
	while p[j]>x do dec(j);
	if i<=j then begin
		swap(a[i],a[j]);
		swap(p[i],p[j]);
		inc(i);
		dec(j);
	end;
until i>j;
if l<j then sort(l,j);
if i<r then sort(i,r);
end; 
procedure sortP(l,r:longint);
var i,j,x:longint;
begin
i:=l;
j:=r;
x:=a[(l+r) div 2];
repeat
	while a[i]<x do inc(i);
	while a[j]>x do dec(j);
	if i<=j then begin
		swap(a[i],a[j]);
		swap(p[i],p[j]);
		inc(i);
		dec(j);
	end;
until i>j;
if l<j then sortP(l,j);
if i<r then sortP(i,r);
end; 
begin
fillchar(p,sizeof(p),0);
read(n);
for i:=1 to n do begin
	fillchar(f,sizeof(f),0);
	read(a[i]);
	s:=inttostr(a[i]);
	ls:=length(s);
	for j:=1 to ls do
		if not f[s[j]] then begin
			inc(p[i]);
			f[s[j]]:=true;
		end;		
end;
sort(1,n);
l:=1;
r:=1;
for i:=2 to n do begin
	if p[i]=p[i-1] then inc(r);
	if (p[i]<>p[i-1]) or (i=n) then begin
		if r-l>0 then sortP(l,r);
		l:=i;
		r:=i;
	end;
end;
for i:=1 to n do write(a[i],' ');
end.
